package practise.alogrithm.hundredInterviewQuestions;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by richard on 6/2/14.
 */
public class FirstAppearanceOnceCharactor {
    public static char getFirstAppearOnceChar(String str){
        if(str.length()==0){
            return ' ';
        }
        Map<Character,Integer> charMap = new HashMap<>(str.length());
        for(char ch:str.toCharArray()){
            charMap.put(ch,charMap.get(ch)==null?1:charMap.get(ch)+1);
        }
        for(char ch:str.toCharArray()){
            if(charMap.get(ch)==1){
                return ch;
            }
        }
        return ' ';
    }

    public static char getFirstAppearOnceCharByByte(String str){
        if(str.length()==0){
            return ' ';
        }
        long charMap =0;
        int mask = 3;
        for(char ch:str.toCharArray()){
            charMap=(mask&(charMap>>>(ch-'a')*2))==0?(charMap|(1<<((ch-'a')*2))):(charMap|(1<<((ch-'a')*2)+1));
        }
        for(char ch:str.toCharArray()){
            if((mask&(charMap>>>(ch-'a')*2))==1){
                return ch;
            }
        }
        return ' ';
    }

    public static void main(String[] args){
        System.out.println("result:"+getFirstAppearOnceCharByByte("abaccdeff"));
    }
}
